<%
local api = require "luci.model.cbi.passwall.api.api"
local auto_switch = api.uci_get_type("auto_switch", "enable", 0)
-%>
<script type="text/javascript">
	//<![CDATA[
	var _status = document.getElementsByClassName('_status');
	for (var i = 0; i < _status.length; i++) {
		var id = _status[i].getAttribute("socks_id");
		XHR.get('<%=api.url("socks_status")%>', {
				index: i,
				id: id
			},
			function(x, result) {
				var index = result.index;
				var div = '';
				var div1 = '<font style="font-weight:bold;" color="green">✓</font>&nbsp';
				var div2 = '<font style="font-weight:bold;" color="red">X</font>&nbsp';
				
				if (result.socks_status) {
					div += div1;
				} else {
					div += div2;
				}
				if (result.use_http) {
					if (result.http_status) {
						div += div1;
					} else {
						div += div2;
					}
				}
				_status[index].innerHTML = div;
			}
		);
	}
	
	var global_id = null;
	var global = document.getElementById("cbi-passwall-global");
	if (global) {
		var node = global.getElementsByClassName("cbi-section-node")[0];
		var node_id = node.getAttribute("id");
		global_id = node_id;
		
		for (var i = 0; i <= 1; i++) {
			var proto = "udp";
			if (i == 0) {
				proto = "tcp";
			}
			var dom_id = node_id + "-" + proto + "_node";
			dom_id = dom_id.replace("cbi-", "cbid-").replace(new RegExp("-", 'g'), ".");
			var node_select = document.getElementById(dom_id);
			var node_select_value = node_select.value;
			if (node_select_value && node_select_value != "nil") {
				var v = document.getElementById(dom_id + "-" + node_select_value);
				node_select.title = v.text;
				
				var new_a = document.createElement("a");
				new_a.innerHTML = "<%:Edit%>";
				new_a.href = "#";
				new_a.setAttribute("onclick", "location.href='" + '<%=api.url("node_config")%>' + "/" + node_select_value + "'");
				
				var log_a = document.createElement("a");
				log_a.innerHTML = "<%:Log%>";
				log_a.href = "#";
				log_a.setAttribute("onclick", "window.open('" + '<%=api.url("get_redir_log")%>' + "?proto=" + proto + "', '_blank')");
				
				node_select.outerHTML = node_select.outerHTML + "&nbsp&nbsp" + new_a.outerHTML + "&nbsp&nbsp" + log_a.outerHTML;
				//node_select.parentNode.insertBefore(new_a, node_select.nextSibling);
			}
		}
	}
	
	var socks = document.getElementById("cbi-passwall-socks");
	if (socks) {
		var socks_enabled_dom = document.getElementById(global_id + "-socks_enabled");
		socks_enabled_dom.parentNode.removeChild(socks_enabled_dom);
		var descr = socks.getElementsByClassName("cbi-section-descr")[0];
		descr.outerHTML = socks_enabled_dom.outerHTML;
		rows = socks.getElementsByClassName("cbi-section-table-row");
		for (var i = 0; i < rows.length; i++) {
			var row = rows[i];
			var id = row.id;
			var dom_id = id + "-node";
			var node = document.getElementById(dom_id);
			var dom_id = dom_id.replace("cbi-", "cbid-").replace(new RegExp("-", 'g'), ".");
			var node_select = document.getElementById(dom_id);
			var node_select_value = node_select.value;
			if (node_select_value && node_select_value != "nil") {
				if (global_id != null && node_select_value.indexOf("tcp") == 0) {
					var num = node_select_value.replace("tcp", "");
					var d = global_id + "-tcp_node";
					d = d.replace("cbi-", "cbid-").replace(new RegExp("-", 'g'), ".");
					var dom = document.getElementById(d);
					var _node_select_value = dom.value;
					if (_node_select_value && _node_select_value != "nil") {
						node_select_value = _node_select_value;
					}
				}
				var v = document.getElementById(dom_id + "-" + node_select_value);
				node_select.title = v.text;
				
				var new_a = document.createElement("a");
				new_a.innerHTML = "<%:Edit%>";
				new_a.href = "#";
				new_a.setAttribute("onclick","location.href='" + '<%=api.url("node_config")%>' + "/" + node_select_value + "'");
				
				node_select.outerHTML = node_select.outerHTML + "&nbsp&nbsp" + new_a.outerHTML;
				//node_select.parentNode.insertBefore(new_a, node_select.nextSibling);
			}
		}
	}
	
	//]]>
</script>